﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GA.Server.Configuration;
using log4net;

namespace GA.Server.Main.Actions
{
    public class StartServerAction : IConsoleAction
    {
        #region IConsoleAction Members

        public string Name
        {
            get { return "StartServer"; }
        }

        public string HelpInfo
        {
            get
            {
                StringBuilder help = new StringBuilder();
                help.AppendLine("StartServer Command:");
                help.AppendLine(" USAGE: StartServer <ServerName> [<Parameter1=Parameter1Value>,<Parameter2=Parameter2Value>,...]");
                help.AppendLine(" DESCRIPTION: Starts the specified server using parameters in the App.config.");
                help.AppendLine(" POSSIBLE ISSUE: Some servers have limited amounts of times that servers can be instantized.");
                help.AppendLine(" POSSIBLE ISSUE: Servers may have parameters that are not in the App.config.");
                return help.ToString();
            }
        }

        public ConsoleActionResult RunAction(string param)
        {
            string serverName = param.Split(' ')[0];
            if (Program.CurrentProgram.AvailableServers.Any((server) => { return server.Name.Equals(serverName, StringComparison.InvariantCultureIgnoreCase); }))
            {
                ServerElement serverElement = Program.CurrentProgram.AvailableServers.First((server) => { return server.Name.Equals(serverName, StringComparison.InvariantCultureIgnoreCase); });
                ServerPropertiesElementCollection properties = serverElement.ServerProperties;
                if (param.Contains(" "))
                {
                    string parameters = param.Substring(param.IndexOf(" "), param.Length - param.IndexOf(" "));
                    foreach (string parameter in parameters.Split(','))
                    {
                        string[] parameterInfo = parameter.Split('=');
                        if (properties.Get(parameterInfo[0]) != null)
                        {
                            properties.Get(parameterInfo[0]).Value = parameterInfo[1];
                        }
                        else
                        {
                            properties.AddProperty(new ServerPropertyElement()
                            {
                                Key = parameterInfo[0],
                                Value = parameterInfo[1]
                            });
                        }
                            
                    }
                }
                LogManager.GetLogger("GA.Server.Main.Actions.StartServer").InfoFormat("Starting server {0}...", serverName);
                Program.CurrentProgram.StartServer(false, serverElement, properties);
                return ConsoleActionResult.CompletedSuccessfully;
            }
            LogManager.GetLogger("GA.Server.Main.Actions.StartServer").ErrorFormat("Could not find server {0}...", serverName);
            return ConsoleActionResult.Error;
        }

        #endregion
    }
}
